package shortlink.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import shortlink.common.convention.result.Result;
import shortlink.common.convention.result.Results;
import shortlink.dto.req.UserLoginReqDTO;
import shortlink.dto.req.UserRegisterReqDTO;
import shortlink.dto.req.UserUpdateReqDTO;
import shortlink.dto.resp.UserLoginRespDTO;
import shortlink.dto.resp.UserRespDTO;
import shortlink.service.UserService;

@RestController
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;
    private final StringRedisTemplate stringRedisTemplate;

    /*查询用户信息*/
    @GetMapping("/api/short-link/admin/user/{username}")
    public Result<UserRespDTO> getUserByUsername(@PathVariable("username") String username) {
        return Results.success(userService.getUserByUsername(username)) ;
    }
    /*检查用户名是否存在*/
    @GetMapping("/api/short-link/admin/user/hasusername")
    public Result<Boolean> hasusername(@RequestParam("username") String username) {
        return Results.success(userService.hasusername(username));
    }


    /*注册用户*/
    @PostMapping("/api/short-link/admin/user/register")
    public Result<Void> register(@RequestBody UserRegisterReqDTO requestParam){
        userService.register(requestParam);
        return Results.success();
    }

    /*用户登录*/
    @PostMapping("/api/short-link/admin/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO requestParam){
        return Results.success(userService.login(requestParam));
    }
    /*检查用户是否已登录*/
    @GetMapping("/api/short-link/admin/user/checklogin")
    public Result<Boolean> checkLogin(@RequestParam("username") String username, @RequestParam("token") String token){
        return Results.success(userService.checklogin(username,token));
    }

    /*退出登录*/
    @PostMapping("/api/short-link/admin/user/loginout")
    public Result<Void> logout(@RequestParam("username") String username,@RequestParam("token") String token){
        userService.logout(username,token);
        return Results.success();
    }

    /*修改用户信息*/
    @PutMapping("/api/short-link/admin/user/update")
    public Result<Void> update(@RequestBody  UserUpdateReqDTO requestParam){
        userService.update(requestParam);
        return Results.success();

    }

}
